﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EZCRS.HCMS.Bll.userinfo;
using EZCRS.HCMS.DataAcl;
using EZCRS.HCMS.Web;

namespace EZCRS.HCMS.Web.manage.company
{
    public partial class company_add : ManageBase
    {
        #region field
        /// <summary>
        /// 公司ID
        /// </summary>
        public int Corpid {
            get { return Convert.ToInt32(this.ViewState["__CompanyCorpid"]); }
            set { this.ViewState["__CompanyCorpid"] = value; }
        }

        /// <summary>
        /// 公司名称
        /// </summary>
        public string Corpname {
            get { return this.txt_Corpname.Text; }
            set { this.txt_Corpname.Text = value; }
        }

        /// <summary>
        /// 英文名称
        /// </summary>
        public string Corpname_e {
            get { return this.txt_Corpname_e.Text; }
            set { this.txt_Corpname_e.Text = value; }
        }

        /// <summary>
        /// 国家
        /// </summary>
        public string corpcountry {
            get { return this.txt_corpcountry.Text; }
            set { this.txt_corpcountry.Text = value; }
        }

        /// <summary>
        /// 省份
        /// </summary>
        public string corpprovince {
            get { return this.txt_corpprovince.Text; }
            set { this.txt_corpprovince.Text = value; }
        }

        /// <summary>
        /// 城市
        /// </summary>
        public string corpcity {
            get { return this.txt_corpcity.Text; }
            set { this.txt_corpcity.Text = value; }
        }

        /// <summary>
        /// 所属行业
        /// </summary>
        public string corptype {
            get { return this.dlist_Corptype.SelectedValue; }
            set { this.dlist_Corptype.SelectedValue = value; }
        }

        /// <summary>
        /// 地址
        /// </summary>
        public string Corpaddress {
            get { return this.txt_Corpaddress.Text; }
            set { this.txt_Corpaddress.Text = value; }
        }

        /// <summary>
        /// 邮政编码
        /// </summary>
        public string Corppost {
            get { return this.txt_Corppost.Text; }
            set { this.txt_Corppost.Text = value; }
        }

        /// <summary>
        /// 联系电话
        /// </summary>
        public string Corptel {
            get { return this.txt_Corptel.Text; }
            set { this.txt_Corptel.Text = value; }
        }

        /// <summary>
        /// 传真
        /// </summary>
        public string Corpfax {
            get { return this.txt_Corpfax.Text; }
            set { this.txt_Corpfax.Text = value; }
        }

        /// <summary>
        /// 电子邮箱
        /// </summary>
        public string Corpemail {
            get { return this.txt_Corpemail.Text; }
            set { this.txt_Corpemail.Text = value; }
        }

        /// <summary>
        /// 姓
        /// </summary>
        public string Confirstname {
            get { return this.txt_Confirstname.Text; }
            set { this.txt_Confirstname.Text = value; }
        }

        /// <summary>
        /// 名
        /// </summary>
        public string Conlastname {
            get { return this.txt_Conlastname.Text; }
            set { this.txt_Conlastname.Text = value; }
        }

        /// <summary>
        /// 性别
        /// </summary>
        public string Congender {
            get { return this.rbl_Congender.SelectedValue; }
            set { this.rbl_Congender.SelectedValue = value; }
        }

        /// <summary>
        /// 电话
        /// </summary>
        public string Conetel {
            get { return this.txt_Contel.Text; }
            set { this.txt_Contel.Text = value; }
        }

        /// <summary>
        /// 电子邮件
        /// </summary>
        public string Conemail {
            get { return this.txt_Conemail.Text; }
            set { this.txt_Conemail.Text = value; }
        }

        /// <summary>
        /// 合同价格代码
        /// </summary>
        public string Corporate {
            get { return this.txt_Corporate.Text; }
            set { this.txt_Corporate.Text = value; }
        }

        /// <summary>
        /// IATA
        /// </summary>
        public string Corpiata {
            get { return this.txt_Corpiata.Text; }
            set { this.txt_Corpiata.Text = value; }
        }

        /// <summary>
        /// 公司状态
        /// </summary>
        public string Corpstatu {
            get { return this.rbl_Corpstatus.SelectedValue; }
            set { this.rbl_Corpstatus.SelectedValue = value; }
        }

        /// <summary>
        /// 集团编号
        /// </summary>
        public int gid {
            get { return base.BasGroupId; }
        }

        /// <summary>
        /// 酒店编号
        /// </summary>
        public int pid {
            get { return base.BasHotelId; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack){
                CheckModule(29);

                this.//ImgReturn.Attributes.Add("onclick", "history.back();return false;");
                //公司所属行业类型
                GetCorptype();
                //公司状态
                GetCompanyStatus();
                //id号
                int _corpid = CFun.RequestNumber("corpid");
                this.ViewState["__CompanyCorpid"] = _corpid;
                if (this.Corpid != 0) {
                    GetFormInfo(Corpid);
                    this.BtnSave.Text = Resources.Site.ButtonModify;
                    this.BtnDel.Visible = true;
                }
            }
            RegisterClientScript();
        }

        /// <summary>
        /// 公司所属行业类型
        /// </summary>
        public void GetCorptype() {
            Bllcompanyinfo AdmC = new Bllcompanyinfo();
                dlist_Corptype.DataSource = AdmC.Company_BussType();
                dlist_Corptype.DataTextField = "typename";
                dlist_Corptype.DataValueField = "typid";
                dlist_Corptype.DataBind();
                dlist_Corptype.Items.Insert(0, new ListItem(Resources.Site.DplistPleaseSelect, "0"));
            }

        /// <summary>
        /// 公司状态
        /// </summary>
        public void GetCompanyStatus() {
            Bllcompanyinfo AdmC = new Bllcompanyinfo();
            rbl_Corpstatus.DataSource = AdmC.Company_Status();
            rbl_Corpstatus.DataTextField = "sname";
            rbl_Corpstatus.DataValueField = "sid";
            rbl_Corpstatus.DataBind();
            if (this.Corpid == 0) {
                rbl_Corpstatus.SelectedIndex = 0;
            }
            else {
                rbl_Corpstatus.SelectedValue = this.Corpstatu;
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e) {
            Bllcompanyinfo AdmC = new Bllcompanyinfo();
            if (AdmC.Company_Add(Corpid,gid,pid, Corpname, Corpname_e, corptype, corpcountry, corpprovince, corpcity, Corpaddress, Corppost, Corptel, Corpfax, Corpemail, Confirstname, Conlastname, Congender, Conetel, Conemail, Corporate, Corpiata, Corpstatu)) {
                //日志类型：19--公司添加 20--公司修改
                int logType = this.Corpid == 0 ? 19 : 20;
                DataAcl.Common.SaveLog(base.BasUserId.ToString(), base.BasHotelId.ToString(), base.BasGroupId.ToString(), logType, "");
                //添加或修改后提示信息
                string succMsg = this.Corpid == 0 ? Resources.Site.SystemAddSuccess : Resources.Site.SystemModifySuccess;
                Response.Write(CFun.AlertMeg(succMsg, "non", "company_list.aspx"));
            }
            else
                Response.Write(CFun.AlertMeg(Resources.Site.SystemError, "back", ""));
        }

        /// <summary>
        /// 获取表单信息
        /// </summary>
        /// <param name="corpid"></param>
        public void GetFormInfo(int corpid) {
            Bllcompanyinfo admc = new Bllcompanyinfo();
            DataRow dr = admc.GetCompanyListOne(corpid);
            this.Corpname = dr["corpname"].ToString();
            this.Corpname_e = dr["corpname_e"].ToString();
            this.corptype = dr["corptype"].ToString();
            this.corpcountry = dr["corpcountry"].ToString();
            this.corpprovince = dr["corpprovince"].ToString();
            this.corpcity = dr["corpcity"].ToString();
            this.Corpaddress = dr["corpaddress"].ToString();
            this.Corppost = dr["corppost"].ToString();
            this.Corptel = dr["corptel"].ToString();
            this.Corpfax = dr["corpfax"].ToString();
            this.Corpemail = dr["corpemail"].ToString();
            this.Confirstname = dr["confirstname"].ToString();
            this.Conlastname = dr["conlastname"].ToString();
            this.Congender = dr["congender"].ToString();
            this.Conetel = dr["contel"].ToString();
            this.Conemail = dr["conemail"].ToString();
            this.Corporate = dr["corporate"].ToString();
            this.Corpiata = dr["corpiata"].ToString();
            this.Corpstatu = dr["corpstatus"].ToString();
        }

        protected void BtnDel_Click(object sender, EventArgs e) {
            Bllcompanyinfo admc = new Bllcompanyinfo();
            bool checkCompanyUser = admc.CheckCompanyUser(Corpid);
            if (checkCompanyUser) {
                Response.Write(CFun.AlertMeg(Resources.Site.CompanyAddDellErr, "non", "company_list.aspx"));
                return;
            }
            if(admc.Company_Del(this.Corpid.ToString())){
                DataAcl.Common.SaveLog(base.BasUserId.ToString(), base.BasHotelId.ToString(), base.BasGroupId.ToString(), 21, "");
                Response.Write(CFun.AlertMeg(Resources.Site.SystemDeleteSuccess,"non","company_list.aspx"));
            }
            else{
                Response.Write(CFun.AlertMeg(Resources.Site.SystemError, "back", ""));
            }
        }

        /// <summary>
        /// 注册脚本事件
        /// </summary>
        private void RegisterClientScript() {
            string confirmDelMsg = Resources.Site.SystemConfirmDelete;
            System.Text.StringBuilder builder = new System.Text.StringBuilder(600);
            builder.Append("function DoConfirmDel(){");
            builder.AppendFormat("var doCon = window.confirm(\"{0}\");", confirmDelMsg);
            builder.Append("return doCon;");
            builder.Append("}");
            string GenUniqueKey = Guid.NewGuid().ToString().Replace("-", string.Empty);
            Page.ClientScript.RegisterClientScriptBlock(typeof(String), GenUniqueKey, builder.ToString(),true);
        }
    }
}
